package javax.xianfeng.drools.controller;

import javax.xianfeng.drools.model.Address;

import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @since 2020/04/07 22:41:58
 */
@RestController
@RequestMapping("/demo")
public class AddressController {

	@Autowired
	private KieSession kieSession;

	@ResponseBody
	@RequestMapping(value = "/address/{postCode}", method = RequestMethod.GET)
	public void test(@PathVariable(value = "postCode") String postCode) {
		// 以下的数据可以从数据库来，这里写死了
		Address address = new Address();
		address.setPostcode(postCode);
		// 使用规则引擎
		kieSession.insert(address);
		int ruleFiredCount = kieSession.fireAllRules();
		System.out.println("触发了" + ruleFiredCount + "条规则");
		System.out.println("---------------------------------");
	}

}
